<?php
/**
 * User:于小涛
 * Date:2020/2/16
 * Time:17:36
 */

use app\admin\model\rbac\OpLog;
use think\response\Json;

/**
 * 后台统一返回处理函数
 * User:于小涛
 * Date:2020/2/16
 * Time:17:41
 * @param array $arr_data
 * @param string $str_code_name
 * @param string $str_msg
 * @param int $int_http_code
 * @param array $arr_other_data
 * @return Json
 */
function output_data($arr_data=array(),$str_code_name='success',$str_msg='',$int_http_code=200,$arr_other_data=array()){
    $str_code_name = $str_code_name??'success';
    $arr_code=config("statuscode.".$str_code_name);
    $arr_data_re=array(
        'code'=>$arr_code['code'],
        'msg'=>empty($str_msg)?$arr_code['msg']:$str_msg,
        'data'=>$arr_data,
        'layuicode'=>0,
    );
    if(env('APP_DEBUG')) {
        $arr_other_data['log'] = \think\facade\Log::getLog();
    }
    $arr_data_re=array_merge($arr_other_data,$arr_data_re);
    if($str_code_name=='admin-auth-checktokenerror'){
        $arr_data_re['layuicode'] = 1001;
    }
    return json($arr_data_re,$int_http_code);
}

/**
 * 输出分页
 * User:于小涛
 * Date:2020/2/23
 * Time:20:54
 * @param $arr_data
 * @param string $str_code_name
 * @param string $str_msg
 * @param int $int_http_code
 * @return Json
 */
function output_page($arr_data,$str_code_name='success',$str_msg='',$int_http_code=200){
    $arr_other_data=['count'=>$arr_data['total']];
    $arr_data=$arr_data['data'];
    return output_data($arr_data,$str_code_name,$str_msg,$int_http_code,$arr_other_data);
}

function import_output_data($str_code_name='success',$arr_other_data=array()){
    $str_code_name = $str_code_name??'success';
    $arr_code=config("statuscode.".$str_code_name);
    $arr_data_re=array(
        'code'=>$arr_code['code'],
        'reason'=>empty($str_msg)?$arr_code['msg']:$str_msg,
    );
    $arr_data_re=array_merge($arr_other_data,$arr_data_re);
    return json($arr_data_re);
}
/**
 * 输出菜单
 * User:于小涛
 * Date:2020/2/21
 * Time:10:42
 */
function show_menu($arr_auth,$int_deep=0){
    $int_deep++;
    $str_html='';
    $tmp_i=1;
    foreach($arr_auth as $arr_item){
        if($arr_item['is_lock']!=1){
            continue;
        }
        if($int_deep==1){
            $str=$tmp_i==1?'layui-nav-itemed':'';//第一个菜单默认打开
            $str_child_html="<li data-name='home' class='layui-nav-item {$str}'><a href='javascript:;' lay-tips='{$arr_item['name']}' lay-direction='2'><i class='layui-icon {$arr_item['icon']}'></i><cite>{$arr_item['name']}</cite></a>";
            if(!empty($arr_item['child'])){
                $str_child_html.=show_menu($arr_item['child'],$int_deep);
            }
            $str_child_html.="</li>";
            $str_html.=$str_child_html;
            $tmp_i++;
        }else{
            //如果是系统管理员，不显示权限菜单
            $arr_admin_info=session('admin');
            if($arr_admin_info['type']!=0&&$arr_item['url']=='/admin/rbac/authGetList'){
                continue;
            }
            if(!empty($arr_item['url'])){
                $str_url="lay-href='".$arr_item['url']."'";
            }else{
                $str_url="href='javascript:;'";
            }
            $str_child_html="<dl class='layui-nav-child'><dd data-name='console'><a {$str_url}>{$arr_item['name']}</a>";
            if(!empty($arr_item['child'])){
                $str_child_html.=show_menu($arr_item['child'],$int_deep);
            }
            $str_child_html.="</dd></dl>";
            $str_html.=$str_child_html;
        }
    }
    return $str_html;
}

/**
 * 操作日志方法
 * User:于小涛
 * Date:2020/2/21
 * Time:10:47
 * @param $str_model_key 模块
 * @param $str_op_type 类型
 * @param $str_content 内容
 */
function op_log($str_model_key,$str_op_type,$str_content){
    $arr_admin_info=session('admin');
    $arr_oplog_config=config("oplog");
    $obj_op_log_model = new OpLog();
    $arr_data=array(
        'admin_id'=>$arr_admin_info['id'],
        'admin_name'=>$arr_admin_info['name'],
        'module'=>$arr_oplog_config['module'][$str_model_key]['name'],
        'op_type'=>$arr_oplog_config['action'][$str_op_type]['name'],
        'detail'=>$str_content,
    );
    $obj_op_log_model->add($arr_data);
}

/**
 * 通过路由，检查是否有该路由权限
 * User:于小涛
 * Date:2020/2/24
 * Time:17:21
 * @param $str_auth_url
 * @param bool $bool_auto_system_admin
 * @return bool
 */
function check_auth_by_route($str_auth_url,$bool_auto_system_admin=true){
    return \app\admin\business\rbac\Auth::checkAuthByRoute($str_auth_url,$bool_auto_system_admin);
}